<?php
require_once("test_7_common.php");

$userId = $user?$user["Id"]:null;
$pageSize = 3;
$pageIndex = isset($_GET["pageIndex"])?$_GET["pageIndex"]:1;
$queryTitle = isset($_GET["Title"])?$_GET["Title"]:null;   //用于精准查询
$queryAuthor = isset($_GET["Author"])?$_GET["Author"]:null;   //用于精准查询
$queryMinDate = isset($_GET["minDate"])?$_GET["minDate"]:null;   //用于精准查询
$queryMaxDate = isset($_GET["maxDate"])?$_GET["maxDate"]:null;   //用于精准查询

$where = "";
//$hasWhere = false;

if($queryTitle){
    /*if(!$hasWhere){
        $sql .=" WHERE ";
        $hasWhere = true;
    }*/
    $where .= "Title like ('%$queryTitle%')";
    //$sql = "SELECT * FROM article WHERE Title like('%$queryTitle%')";
}

if($queryAuthor){
    /*if(!$hasWhere){
        $sql .=" WHERE ";
        $hasWhere = true;
    }*/
    if($where){
        $where .= " AND ";
    }
    $where .= "AuthorName like ('%$queryAuthor%')";
    //$sql = "SELECT * FROM article WHERE Title like('%$queryTitle%')";
}

if($queryMinDate){
    if($where){
        $where .= " AND ";
    }
    $where .=" CreateTime >= '$queryMinDate'";
}

if($queryMaxDate){
    if($where){
        $where .= " AND ";
    }
    $where .=" CreateTime <= '$queryMaxDate'";
}

if($pageIndex<1){
    $pageIndex = 1;
}

$pageCount = 0;
$startRow = ($pageIndex-1)*$pageSize;
$sql = "SELECT * FROM article";
if($where){
    $sql .=" WHERE $where";
}

$conn = createDb();
$rows = [];
$sql .=" LIMIT $startRow,$pageSize";
$rs = $conn->query($sql);
echo $conn->error;
while($row = $rs->fetch_assoc()){
    $rows[] = $row;
}
$rs->close();

$total = 0;
$sql = "SELECT count(Id) AS t FROM article";
if($where){
    $sql .=" WHERE $where";
}
$rs = $conn->query($sql);
echo $conn->error;
$total = $rs->fetch_assoc()["t"];
$pageCount = ceil($total/$pageSize);


require_once("test_7_header.php");
?>

                <h3>文章列表</h3>
                <a href="test_7_article_add.php">新建文章</a>
                <form action="" class="filter" method="GET">
                    <span>
                        <label>标题</label>
                        <input type="text" name="Title" id="Title" value="<?=$queryTitle?>"/>
                    </span>
                    <span>
                        <label>作者</label>
                        <input type="text" name="Author" value="<?=$queryAuthor?>" id="Title"/>
                    </span>
                    <span>
                        <label>时间</label>
                        <input type="text" name="minDate" value="<?=$queryMinDate?>"/>
                        - <input type="text" name="maxDate" value="<?=$queryMaxDate?>"/>
                    </span>
                    <input type="submit" name="submit" value="搜索"/>
                </form>

                <span class="btn" id="listBtn">列表</span>
                <span class="btn" id="cardBtn">卡片</span>
                <div id="panel" class="list">
                    <div class="table">
                        <div class="thead">
                            <div class="tr">
                                <div class="th" style="width: 200px;"><span class="">标题</span></div>
                                <div class="th">作者</div>
                                <div class="th">创建时间</div>
                                <div class="th">操作</div>
                            </div>
                        </div>
                        <div class="tbody">
                            <?php foreach($rows as $row){?>
                                <div class="tr">
                                    <div class="td Title">
                                        <?=$row["Title"]?>
                                    </div>
                                    <div class="td">
                                        <?=$row["AuthorName"]?>
                                    </div>
                                    <div class="td">
                                        <?=$row["UpdateTime"]?> ~ 
                                        <?=$row["CreateTime"]?>
                                    </div>
                                    <div class="td">
                                        <a href="test_7_article_particular.php?Id=<?=$row["Id"]?>">详细</a>
                                        <?php if($user["Id"]===$row["AuthorID"]) {?>
                                        <a href="test_7_article_modify.php?Id=<?=$row["Id"]?>">修改</a>
                                        <a href="test_7_article_delete.php?Id=<?=$row["Id"]?>" class="delete">删除</a>
                                        <?php }?>
                                    </div>
                                </div>
                            <?php } ?>
                        </div>
                        <div id="recordInfo">
                            共<?=$total ?>条记录，<?=$pageCount ?>页，当前为<input type="text" value="<?=$pageIndex ?>" onblur="jumpTo(this.value)"/>页，
                                <a href = "test_7_article_list.php?<?=makePageUrl(1)?>">首页</a> <
                                    <?php for($i = 1;$i<=$pageCount;$i++){ ?>
                                        <a href="test_7_article_list.php?<?=makePageUrl($i)?>"><?=$i ?></a>
                                    <?php }?>
                                > 
                            <a href = "test_7_article_list.php?<?=makePageUrl($pageCount)?>">尾页</a>
                        </div>
                        <script type="text/javascript">
                                function jumpTo(pageIndex){
                                    var url ="?";
                                    var form = document.getElementById("form");
                                    var data = getData(form,{});
                                    for (var n in data){
                                        url += "&" + n +"=" + data[n];
                                    }
                                    url += "&pageIndex=" + pageIndex;
                                    location.href = url;
                                }
                                function getData(form,data){
                                    for( let i=0 ,j=form.childNodes.length; i<j;i++){
                                        let child = form.childNodes[i];
                                        if(child.tagName == "INPUT")    data[child.name] = child.value;
                                        if(child.childNodes && child.childNodes.length) getData(child,data);
                                    }
                                    return data;
                                }
                        </script>
                    </div>
                </div>
                
                <script type="text/javascript">
                    var deleteLinks = document.getElementsByClassName("delete");
                        for(var i=0,j=deleteLinks.length;i<j;i++){
                            var link = deleteLinks[i];
                            link.onclick = function(e){
                                e = e || event;
                                var confirmd = confirm("你确定要删除吗？");
                                if(!confirmd){
                                    e.preventDefault();
                                    return false;
                                }
                            }
                        }
                </script>
<?php require_once("test_7_footer.php");?>